// components/area/area.js
import service from '../../utils/request'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    title: {
      type: String,
      default: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    addressCity:null,
    multiArray: [],  // 三维数组数据
    multiIndex: [0, 0, 0], // 默认的下标,
    province_name:null,
    title: '预约充电',
    city_name:null,
    area_name:null,
    province_list: [],
    city_list: [],
    area_list: [],
    
  },
  ready() {
    this.getDay()
  },
  // observers: {
  //   'province_name, city_name, area_name': function(province_name, city_name, area_name) {
  //     console.log('数据编号')
  //     this.setData({
  //       multiArray: [province_name, city_name, area_name]
  //     })
  //   }
  // },
  /**
   * 组件的方法列表
   */
  methods: {
    getDay() {
        let that = this
        let nameArr = ['今天', '明天']  //获取名称
        var date = new Date() //今天
        let  moDay = new Date(new Date().getTime()+ 24*60*60*1000) //，明天
        var one = date.getFullYear() +'-'+ (date.getMonth()+1) +'-'+ date.getDate();
        var two = moDay.getFullYear() +'-'+ (moDay.getMonth()+1) +'-'+ moDay.getDate();
        let pro = [{id: one}, {id : two}]
        console.log('nameArr', pro)
        that.setData({
          multiArray:[nameArr,[],[]], //更新三维数组  
          province_name:nameArr,  //省级全部名称
          province_list: pro
        })
        let defaultCode = Object.keys(nameArr)[0] //使用第一项当作参数获取市级数据
        console.log('defaultCode',defaultCode)
        setTimeout(() => {
          console.log('getHour')
          that.getHour(defaultCode) //获取市区数据
        }, 300);
    },
    
    getHour(pid){
      console.log('hoer', pid)
      let that = this
      let hour = [], i = 0
      let endDay 
      if(pid == 0){
        //今天划去过去的时间
        i = parseInt(new Date().getHours())
        endDay = 23
      }else{
        let shi = new Date().getHours()
        endDay = 24 - shi
      }
      let list =[]
      for( i; i<= endDay ; i++){
        hour.push(i+'时') 
        list.push({id: i})
      }
      this.setData({
        multiArray:[that.data.province_name,hour,[]],  //更新后[['广东','北京'],['潮州'，'汕头','揭阳'],[]]
        city_name:hour,   //市级名称
        city_list: list,
        currentProvinceId: pid
      })
      let defaultCode = Object.keys(hour)[0] //获取第一个小时的区级数据
      
      setTimeout(() => {
        console.log('getMinute')
        that.getMinute(defaultCode) //获取区域数据
      }, 300);
    },
    getMinute(pid){
      let that = this
      let min = [], i = 0, list = []
      if(this.data.currentProvinceId == 0 && pid == 0){
        //今天划去过去的时间
        i = parseInt(new Date().getMinutes())
      }
      console.log('pid', pid, this.data.currentProvinceId, i)
      for(i; i<= 59 ; i++){
        if(i < 10){
          min.push(i+"分")
        }else{
          min.push(i+"分") 
        }
        list.push({id: i})
      }
      that.setData({
        multiArray:[that.data.province_name,that.data.city_name, min],
        area_name:min,
        area_list: list
      })
      console.log('multiArray', this.data.multiArray)
    },


    //picker确认选择地区
  bindRegionChange:function(e){
    // console.log('e', this.data.city_list, e.detail.value)
    // 因为在获取省中 北京只有一个选项，导致获取不了北京》北京》第一个
      
      // if(e.detail.value[1]==null || e.detail.value[2]==null){ 
      //   this.setData({
      //     multiIndex: e.detail.value,  
      //     addressCity: [this.data.province_name[e.detail.value[0]], this.data.city_name[e.detail.value[1]], this.data.area_name[e.detail.value[2]]],
      //     selectProvinceId:  this.data.province_list[e.detail.value[0]].id,
      //     selectCityId: this.data.city_list[0].id,
      //     selectAreaId: this.data.area_list[0].id
      //   })
      // }else{
      //   this.setData({
      //     multiIndex: e.detail.value,  
      //     addressCity: [this.data.province_name[e.detail.value[0]], this.data.city_name[e.detail.value[1]], this.data.area_name[e.detail.value[2]]],
      //     selectProvinceId:  this.data.province_list[e.detail.value[0]].id,
      //     selectCityId: this.data.city_list[e.detail.value[1]].id,
      //     selectAreaId: this.data.area_list[e.detail.value[2]].id
      //   })
      // }
      
      let selectProvinceId = this.data.province_list[e.detail.value[0]].id
      let selectCityId = this.data.city_list[e.detail.value[1]].id
      let selectAreaId = this.data.area_list[e.detail.value[2]].id
      let option = `${selectProvinceId}:${selectCityId}:${selectAreaId}`
      console.log(option)
      this.triggerEvent('confirmData', option)
  },
      //滑动地区组件
  bindRegionColumnChange:function(e){
    console.log(e.detail)
    let that = this
    let column = e.detail.column  //当前改变的列
    let data = {
      multiIndex:JSON.parse(JSON.stringify(that.data.multiIndex)),
      multiArray:JSON.parse(JSON.stringify(that.data.multiArray))
    }
    data.multiIndex[column] = e.detail.value  //第几列改变了就是对应multiIndex的第几个，更新他
    switch(column){
      case 0 :  //第一列改变，天改变
          var currentProvinceId = Object.keys(that.data.province_name)[e.detail.value]
          console.log('currentProvinceId', currentProvinceId, currentProvinceId)
          that.getHour(currentProvinceId)   //获取当前id下的市级数据
           data.multiIndex[1] = 0    //将市默认选择第一个
           data.multiIndex[2] = 0
           data.currentProvinceId = currentProvinceId
                
        break
      case 1 :  //第二列改变，市级改变
        let currentCityId = Object.keys(that.data.city_name)[e.detail.value]
        setTimeout(() => {
          that.getMinute(currentCityId) //获取区域
        }, 200);
        data.multiIndex[2] = 0  //区域默认第一个
        break
    }
    that.setData(data)  //更新数据
  },
  }
})
